package com.dindang.checkline.app.utils;

import android.text.TextUtils;
import android.util.Base64;
import android.widget.EditText;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Created by cxw on & 2018/11/8
 */
public class MyUtils {

    public static String userid = "887";
    public static String phone = "18240695864";
    public static String username = "张惊涛";

    public static String sign(String method, String obj){
        String sign = ArmsUtils.encodeToMD5( method + "&"+ obj.toString() + "&key=ohQk0moiQTbgq5Ag");
        LogUtils.debugInfo("api_method", method);
        LogUtils.debugInfo("api_json", obj);
        LogUtils.debugInfo("api_sign", sign);
        return sign;
    }

    //设置EditText可输入和不可输入状态
    public static void editTextable(EditText editText, boolean editable) {
        if (!editable) { // disable editing password
            editText.setFocusable(false);
            editText.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
            editText.setClickable(false); // user navigates with wheel and selects widget
        } else { // enable editing of password
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(true);
        }
    }

    /**
     * 将图片转换成Base64编码的字符串
     * @param path
     * @return base64编码的字符串
     */
    public static String imageToBase64(String path){
        if(TextUtils.isEmpty(path)){
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try{
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data,Base64.DEFAULT);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(null !=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    //获取时间差
    public static String getTimeDuration(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
         long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
         long sec = diff % nd % nh % nm / ns;
        String str_tian = day+"天";
        String str_hour = day+"小时";
        String str_min = day+"分钟";
        String str_sec = sec+"秒";
        if(day==0){
            str_tian = "";
        }
        if(hour==0){
            str_hour = "";
        }
        if(min==0){
            str_min = "";
        }
        if(sec==0){
            str_sec = "";
        }
        return str_tian +str_hour + str_min+str_sec;
    }


    //获取时间差
    public static String getTimeDuration(long diff) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        String str_tian = day+"天";
        String str_hour = hour+"小时";
        String str_min = min+"分钟";
        String str_sec = sec+"秒";
        if(day==0){
            str_tian = "";
        }
        if(hour==0){
            str_hour = "";
        }
        if(min==0){
            str_min = "";
        }
        if(sec==0){
            str_sec = "";
        }
        return str_tian +str_hour + str_min+str_sec;
    }

    //倒计时，精确到秒
    public static String getTimeCount(long diff) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        String str_tian = day+"天";
        String str_hour = hour+"小时";
        String str_min = min+"分钟";
        String str_sec = sec+"秒";
        if(day==0){
            str_tian = "";
        }
        if(hour==0){
            str_hour = "";
        }
        if(min==0){
            str_min = "";
        }
        if(sec==0){
            str_sec = "";
        }
        return str_tian +str_hour + str_min+str_sec;
    }
}
